﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using LaughingDog.Components;
using LaughingDog.Input;
using LaughingDog.Managers;
using Microsoft.Xna.Framework.Input;

namespace Demo
{
    public class ControlledCamera : GameObject
    {
        private Single mZoomAmount = 0.01f;
        private Single mRotateAmount = 0.5f;
        private Single mMoveSpeed = 1f;

        public ControlledCamera()
        {
            AddComponent<Camera>();

            // Add a bunch of InputMappings for all the desired actions.
            SetUpInput();
        }       

        public override void Update()
        {
            // Check the InputMappings and perform the required actions.
            HandleInput();

            base.Update();
        }

        private void SetUpInput()
        {
            InputMapping zoomIn = InputManager.Instance.AddInputMapping("ZoomIn");
            zoomIn.AddInput(Keys.Z);
            zoomIn.AddInput(Keys.PageUp);

            InputMapping zoomOut = InputManager.Instance.AddInputMapping("ZoomOut");
            zoomOut.AddInput(Keys.X);
            zoomOut.AddInput(Keys.PageDown);

            InputMapping rotateLeft = InputManager.Instance.AddInputMapping("RotateLeft");
            rotateLeft.AddInput(Keys.Q);
            rotateLeft.AddInput(Keys.RightShift);

            InputMapping rotateRight = InputManager.Instance.AddInputMapping("RotateRight");
            rotateRight.AddInput(Keys.E);
            rotateRight.AddInput(Keys.End);

            InputMapping moveUp = InputManager.Instance.AddInputMapping("MoveUp");
            moveUp.AddInput(Keys.W);
            moveUp.AddInput(Keys.Up);

            InputMapping moveDown = InputManager.Instance.AddInputMapping("MoveDown");
            moveDown.AddInput(Keys.A);
            moveDown.AddInput(Keys.Down);

            InputMapping moveLeft = InputManager.Instance.AddInputMapping("MoveLeft");
            moveLeft.AddInput(Keys.A);
            moveLeft.AddInput(Keys.Left);

            InputMapping moveRight = InputManager.Instance.AddInputMapping("MoveRight");
            moveRight.AddInput(Keys.D);
            moveRight.AddInput(Keys.Right);
        }

        private void HandleInput()
        {
            if (InputManager.Instance.InputPressed("ZoomIn"))
                Camera.Zoom += mZoomAmount; // Every GameObject can have only one Camera component attached to it, this would be null if there wasn't one.

            if (InputManager.Instance.InputPressed("ZoomOut"))
                Camera.Zoom -= mZoomAmount;

            if (InputManager.Instance.InputPressed("RotateLeft"))
                Transform.Rotate(-mRotateAmount); // Every GameObject has a Transform component by default.

            if (InputManager.Instance.InputPressed("RotateRight"))
                Transform.Rotate(mRotateAmount);

            if (InputManager.Instance.InputPressed("MoveUp"))
                Transform.Translate(0, -mMoveSpeed);

            if (InputManager.Instance.InputPressed("MoveDown"))
                Transform.Translate(0, mMoveSpeed);

            if (InputManager.Instance.InputPressed("MoveLeft"))
                Transform.Translate(-mMoveSpeed, 0);

            if (InputManager.Instance.InputPressed("MoveRight"))
                Transform.Translate(mMoveSpeed, 0);
        }
    }
}
